package com.stedu.controller;

import com.stedu.bean.College;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/college")
public class CollegeController {
    @Autowired
    private CollegeService collegeService;

    @PostMapping
    public RespBean insert(@RequestBody @Validated College college) throws SteduException {
        //添加
        if (collegeService.insert(college)){
            return RespBean.ok("添加成功~~~");
        }else {
            return RespBean.error("添加失败，请重新添加");
        }
    }

    @DeleteMapping("/{zid}")
    public RespBean delete(@PathVariable("zid") Integer zid) throws SteduException {
        //删除
        if (collegeService.delete(zid)){
            return RespBean.ok("删除成功~~~");
        }else{
            return RespBean.error("删除失败,该专业下有学生");
        }
    }
    @PutMapping
    public RespBean update(@RequestBody @Validated College college) throws SteduException {
        if (collegeService.update(college)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }
    @GetMapping
    public RespBean selectAll() throws SQLException {
        List<College> colleges = collegeService.selectAll();
        return  RespBean.ok("", colleges);

    }
    @GetMapping("/{zid}")
    public RespBean selectByZid(@PathVariable("zid") Integer zid) {
        College college = collegeService.selectByZid(zid);
        return RespBean.ok("", college);

    }
}
